package com.apexbrain.apexbrainserver.controller;

import com.apexbrain.apexbrainserver.mapper.QuestionAnswerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * tb_question_answer表的controller
 */
@Controller
@RequestMapping("/question_answer")
public class QuestionAnswerController
{
    @Autowired
    private QuestionAnswerMapper questionAnswerMapper;

    /**
     * 插入用户回答答案
     * @param user_id 用户的用户名
     * @param question_id 问题的id
     * @param answer 用户的答案
     * @return 插入是否成功
     */
    @GetMapping("/add_answer")
    @ResponseBody
    public boolean addAnswer(@RequestParam String user_id, @RequestParam int question_id, @RequestParam String answer)
    {
        return questionAnswerMapper.addAnswer("'" + user_id + "'", question_id, "'" + answer + "'");
    }

    /**
     * 获得用户的回答，如果还没回答则返回null
     * @param user_id 用户的用户名
     * @param question_id 问题的id
     * @return answer字段
     */
    @GetMapping("/get_answer")
    @ResponseBody
    public String getAnswer(@RequestParam String user_id, @RequestParam int question_id)
    {
        return questionAnswerMapper.getAnswer("'" + user_id + "'", question_id);
    }

    /**
     * 删除用户的对战答题记录
     * @param user_id 用户的用户名
     * @return 删除是否成功
     */
    @GetMapping("/erase_answers")
    @ResponseBody
    public boolean eraseAnswers(@RequestParam String user_id)
    {
        return questionAnswerMapper.eraseAnswers("'" + user_id + "'");
    }
}
